#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "Menu.h"
#include "Student_Func.h"
#include "Admin_Func.h"
#include "Person.h"
#include "Teacher_Func.h"

// ANSI颜色代码
#define COLOR_RESET   "\x1B[0m"
#define COLOR_RED     "\x1B[31m"
#define COLOR_GREEN   "\x1B[32m"
#define COLOR_YELLOW  "\x1B[33m"
#define COLOR_BLUE    "\x1B[34m"
#define COLOR_MAGENTA "\x1B[35m"
#define COLOR_CYAN    "\x1B[36m"
#define COLOR_WHITE   "\x1B[37m"
#define BOLD          "\x1B[1m"
#define UNDERLINE     "\x1B[4m"

// 清除屏幕
void clearScreen() {
    printf("\033[2J\033[1;1H");
}

// 获取终端宽度（简化版）
int getTerminalWidth() {
    // 假设终端宽度为80列，更精确的实现需要使用ioctl
    return 80;
}

// 绘制水平分隔线
void drawLine(int length, char style) {
    for (int i = 0; i < length; i++) {
        printf("%c", style);
    }
    printf("\n");
}

// 居中打印文本
void printCentered(const char *text, int width) {
    int len = strlen(text);
    int padding = (width - len) / 2;
    printf("%*s%s%*s\n", padding, "", text, padding + (len % 2), "");
}

void main_menu() {
    const int width = 60;
    clearScreen();
    
    printf(COLOR_MAGENTA BOLD UNDERLINE);
    drawLine(width, '*');
    printCentered("主菜单", width);
    drawLine(width, '*');
    printf(COLOR_RESET);
    
    printf("\n");
    printf(COLOR_CYAN "  [1] 登录\n" );
    printf("  [2] 注册\n");
    printf("  [3] 退出\n"COLOR_RESET);
    
    printf("\n");
    printf(COLOR_YELLOW);
    drawLine(width, '-');
    printf(COLOR_RESET);
}

// 学校分配账号，老师不用注册
void register_Menu() {
    const int width = 60;
    clearScreen();
    
    printf(COLOR_BLUE BOLD);
    drawLine(width, '=');
    printCentered("注册菜单", width);
    drawLine(width, '=');
    printf(COLOR_RESET);
    
    printf("\n");
    printf(COLOR_GREEN "  [1] 学生账号注册\n" );
    printf("  [2] 退出\n"COLOR_RESET);
    
    printf("\n");
    printf(COLOR_YELLOW);
    drawLine(width, '-');
    printf(COLOR_RESET);
}

void enter_Menu() {
    const int width = 60;
    clearScreen();
    
    printf(COLOR_BLUE BOLD);
    drawLine(width, '=');
    printCentered("登录菜单", width);
    drawLine(width, '=');
    printf(COLOR_RESET);
    
    printf("\n");
    printf(COLOR_GREEN "  [1] 学生登录\n" );
    printf("  [2] 教师登录\n");
    printf("  [3] 管理员登录\n");
    printf("  [4] 退出\n"COLOR_RESET);
    
    printf("\n");
    printf(COLOR_YELLOW);
    drawLine(width, '-');
    printf(COLOR_RESET);
    printf("请选择功能:");
}

//已完成
void Student_Menu() {
    const int width = 60;
    clearScreen();
    
    printf(COLOR_BLUE BOLD);
    drawLine(width, '=');
    printCentered("学生功能菜单", width);
    drawLine(width, '=');
    printf(COLOR_RESET);
    
    printf("\n");
    printf(COLOR_GREEN "  [1] 查看本人信息\n" COLOR_RESET);
    printf("  [2] 修改本人信息\n");
    printf("  [3] 退出\n");
    
    printf("\n");
    printf(COLOR_YELLOW);
    drawLine(width, '-');
    printf(COLOR_RESET);
}

void Teacher_Menu() {
    const int width = 60;
    clearScreen();
    
    printf(COLOR_BLUE BOLD);
    drawLine(width, '=');
    printCentered("教师功能菜单", width);
    drawLine(width, '=');
    printf(COLOR_RESET);
    
    printf("\n");
    printf(COLOR_GREEN "  [1] 查看所有学生\n" );
    printf("  [2] 查找学生\n");
    printf("  [3]录分\n");
    printf("  [4] 退出\n"COLOR_RESET);
    
    printf("\n");
    printf(COLOR_YELLOW);
    drawLine(width, '-');
    printf(COLOR_RESET);
}

void Admin_Menu() {
    const int width = 60;
    clearScreen();
    
    printf(COLOR_BLUE BOLD);
    drawLine(width, '=');
    printCentered("管理员功能菜单", width);
    drawLine(width, '=');
    printf(COLOR_RESET);
    
    printf("\n");
    printf(COLOR_GREEN "  [1] 查看所有学生\n" );
    printf("  [2] 添加学生\n");
    printf("  [3] 查找学生\n");
    printf("  [4] 删除学生\n");
    printf("  [5] 修改学生信息\n");
    printf("  [6] 查看所有教师\n");
    printf("  [7] 添加教师\n");
    printf("  [8] 查找教师\n");
    printf("  [9] 修改教师信息\n");
    printf("  [10] 删除教师\n");
    printf("  [11] 查看所有管理员\n");
    printf("  [12] 退出\n"COLOR_RESET);
    
    printf("\n");
    printf(COLOR_YELLOW);
    drawLine(width, '-');
    printf(COLOR_RESET);
    printf("请选择功能:");
}

void choice_menu(pStudent_t stu_phead,pAdmin_t admin_phead,pTeacher_t teacher_phead)
{
    int num=0;
    main_menu();
    printf("请输入您的选择:");
    scanf("%d",&num);
    printf("\n");
    while(num!=3)
    {
        switch(num)
        {   
            //登录界面
            //1.学生登录
            //2.教师登录
            //3.管理员登录
            //4.退出
            case 1:
                enter_Menu();
                int choice1 = 0;
                scanf("%d",&choice1);
                printf("\n");
                while(choice1!=4)
                {
                    switch(choice1)
                    {
                        case 1:
                            pStudent_t stu_plink=Login_check(stu_phead);
                            if(stu_plink!=NULL)
                            {
                                Student_Menu();
                                int choice2 = 0;
                                scanf("%d",&choice2);
                                printf("\n");
                                while(choice2!=3)
                                {
                                    switch(choice2)
                                    {
                                        //查看本人信息
                                        case 1:
                                            Print_One_Student(stu_plink);
                                            wait_for_any_key();
                                            break;
                                        //修改本人信息
                                        case 2:
                                            Simplied_Student_Modify(stu_plink);
                                            wait_for_any_key();
                                            break;
                                        case 3:
                                            printf("已退出学生功能菜单\n");
                                            wait_for_any_key();
                                            break;
                                        default:
                                            printf("请输入正确的选择:");
                                            wait_for_any_key();
                                            break;
                                    }
                                    printf("\n");
                                    Student_Menu();
                                    scanf("%d",&choice2);
                                    printf("\n");
                                }
                            }
                            break;

                            //教师登录
                        case 2:
                            pTeacher_t teacher_plink=Teacher_Login_check(teacher_phead);
                            if(teacher_plink!=NULL)
                            {
                                Teacher_Menu();
                                int choice3 = 0;
                                Teacher_Menu();
                                printf("请输入您的选择:");
                                scanf("%d",&choice3);
                                while(getchar()!='\n');
                                while(choice3!=4)
                                {
                                    switch(choice3)
                                    {
                                        case 1:
                                            Print_Student_List(stu_phead);
                                            wait_for_any_key();
                                            break;
                                        case 2:
                                            Admin_Find_Student(stu_phead);
                                            wait_for_any_key();
                                            break;
                                        case 3:
                                            Teacher_Modify_Student(stu_phead);
                                            wait_for_any_key();
                                            break;
                                        case 4:
                                            break;
                                        default:
                                            printf("请输入正确的选择:");
                                            wait_for_any_key();
                                            break;
                                    }
                                    printf("\n");
                                    Teacher_Menu();
                                    printf("请输入您的选择:");
                                    scanf("%d",&choice3);
                                    while(getchar()!='\n');
                                }
                                break;
                            }
                            break;
                            //管理员登录
                        case 3:
                            pAdmin_t admin_plink=Admin_Login_check(admin_phead);
                            if(admin_plink!=NULL)
                            {
                                int choice3 = 0;
                                Admin_Menu();
                                scanf("%d",&choice3);
                                while(choice3!=12)
                                {
                                    switch(choice3)
                                    {
                                        case 1:
                                            Admin_Print_Student_List(stu_phead);
                                            wait_for_any_key();
                                            break;
                                        case 2:
                                            Student_Head_Insert(stu_phead,Admin_Create_New_Student_Scanf());
                                            wait_for_any_key();
                                            break;
                                        case 3:
                                            Admin_Find_Student(stu_phead);
                                            wait_for_any_key();
                                            break;
                                        case 4:
                                            Admin_Delete_Student(stu_phead);
                                            wait_for_any_key();
                                            break;
                                        case 5:
                                            Admin_Modify_Student(stu_phead);
                                            wait_for_any_key();
                                            break;
                                        case 6:
                                            Admin_Print_Teacher_List(teacher_phead);
                                            wait_for_any_key();
                                            break;
                                        case 7:
                                            Teacher_Head_Insert(teacher_phead,Create_new_Teacher_scanf());
                                            wait_for_any_key();
                                            break;
                                        case 8:
                                            Admin_Find_Teacher(teacher_phead);
                                            wait_for_any_key();
                                            break;
                                        case 9:
                                            Admin_Modify_Teacher(teacher_phead);
                                            wait_for_any_key();
                                            break;
                                        case 10:
                                            Admin_Delete_Teacher(teacher_phead);
                                            wait_for_any_key();
                                            break;
                                        case 11:
                                            Print_Admin_List(admin_phead);
                                            wait_for_any_key();
                                            break;
                                        case 12:
                                            printf("已退出管理员功能菜单\n");
                                            wait_for_any_key();
                                            break;
                                        default:
                                            printf("请输入正确的选择:");
                                            break;
                                    }
                                    printf("\n");
                                    Admin_Menu();
                                    scanf("%d",&choice3);
                                    while(getchar()!='\n');
                                    printf("\n");
                                }
                            }
                            break;
                        case 4:
                            break;
                        default:
                            printf("请输入正确的选择:");
                            break;
                    }
                    printf("\n");
                    enter_Menu();
                    printf("请输入您的选择:");
                    scanf("%d",&choice1);
                    printf("\n");
                }
                break;
            case 2:
                //注册界面，只有一个学生注册
                register_Menu();
                printf("请输入您的选择:");
                int choice = 0;
                scanf("%d",&choice);
                printf("\n");
                while(choice!=2)
                {
                    printf("\n");
                    switch(choice)
                    {
                        case 1:
                            Student_Head_Insert(stu_phead,Create_new_node_scanf());
                            wait_for_any_key();
                            break;
                        case 2:
                            printf("已退出注册界面\n");
                            break;
                        default:
                            printf("请输入正确的选择:");
                            break;
                    }
                    register_Menu();
                    printf("请输入您的选择:");
                    scanf("%d",&choice);
                    printf("\n");
                }
                break;
            case 3:
                break;
            default:
                printf("请输入正确的选择:");
                break;
        }
        printf("\n");
        main_menu();
        printf("请输入您的选择:");
        scanf("%d",&num);
    }
}